{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "914e01c7-6f04-47ef-b4cd-5243044d1afd",
   "metadata": {},
   "source": [
    "<center><h1><a href=\"https://challenge.xfyun.cn/topic/info?type=battery-material&option=ssgy&ch=dw24_AtTCK9\">基于热力学定律的电池材料生产参数动态调控挑战赛</a></h1></center>\n",
    "\n",
    "\n",
    "# 一、赛事背景\n",
    "\n",
    "随着低空空域的逐步开放，低空经济产业将具有更广阔的市场前景。近年来，以通用航空产业为主体的低空经济得到快速发展，锂离子电池已成为通用航空产业的新型动力源。但电池本身的热量与环境的高温叠加，将严重影响电池的寿命，材料是锂离子电池的重要组成部分，对电池的工作电压、容量、寿命、安全性等性能有很大的影响，其成本占电池总成本的 30%~40%，全国锂离子电池材料产值达到535亿元。解决锂离子电池在高温环境下的安全性问题，需从解决材料中的不稳定性，提高电池材料、电芯电池组制造工艺的一致性等问题入手。\n",
    "\n",
    "材料的主要生产设备是电加热辊道窑，辊道窑烧结过程耦合严重、过程变量多、变量之间关系复杂，温度场、浓度场难以精确控制；烧结工况波动大、多温段之间相互影响。而建立多温段烧结制度协同优化方法，研究烧结过程的数字化建模，通过辊道窑空间温度推测材料产品内部温度，设计烧结过程的温度场和浓度场的最优控制律，搭建材料制备过程运行平台，有望最终实现该过程的效率提升和协同优化，达到提高材料产品一致性，降低生产能耗的目标。同时确保通用航空产业锂离子电池安全服役，而且对于提高我国低空经济产业发展具有重要意义。\n",
    "\n",
    "# 二、赛事任务\n",
    "\n",
    "初赛（任务一）：辊道窑生产过程中会产生大量的生产调控参数，这些参数共同决定辊道窑中升温段、恒温段、冷却段的温度稳定性。本次大赛提供了辊道窑17个温区的实际生产数据，每个温区有加热棒上部设定温度、加热棒下部设定温度、上部测温点实际温度、下部测温点实际温度、匣钵上部实际温度、匣钵下部实际温度、进气口的设定进气流量7种数据。选手需根据提供的样本参数，结合能量守恒方程（公式a）以及提供的模型参数建立辊道窑加热棒设定温度、进气口进气流量与实际温度的时空控制模型，并进行模型验证。\n",
    "\n",
    "公式a：![img](https://openres.xfyun.cn/xfyundoc/2024-06-29/e172d56c-9c75-4aa1-a7b5-944d92cc244c/1719629926349/105.png)\n",
    "\n",
    "其中，T为温度，是T(x,z,t)的简写；t为时间；ρ与c分别为密度与比热容，其具体数值与空间位置相关：在匣钵区域（图中蓝色框中区域）ρ与c取多孔介质的密度与比热容；在所有其他区域，ρ与c取窑内气体的密度与比热容；u与v分别为窑炉内气体在x轴和z轴方向上的速度；k为导热系数，其具体数值与空间位置相关：在匣钵区域k取多孔介质的导热系数，其他区域取窑内气体的导热系数。（辊道窑物理模型参数如图所示；炉内气体的密度为1.225kg/m³、比热容为1006.43J/(kg*K)、导热系数为202.4W/(m*K)；匣钵内为多孔介质，密度为2300kg/m³、比热容为767J/(kg*K)、导热系数为0.66W/(m*K)）。\n",
    "\n",
    "辊道窑二维模型如图所示，底部进气口直径为15mm、排气口直径为30mm，进气口、排气口和加热棒在同一垂直线上，匣钵移动速度为0.00011m/s；所有加热棒及测温点均视为一个点，无物理建模。每个温区只包含一个上部测温点、一个下部测温点、一个匣钵上部测温点、一个匣钵下部测温点、一个底部进气口。\n",
    "\n",
    "![img](https://openres.xfyun.cn/xfyundoc/2024-06-29/ed747e70-94e1-4121-87a3-49d1a47e0605/1719630051714/106.png)\n",
    "\n",
    "\n",
    "## 1.数据说明\n",
    "\n",
    "本次比赛为参赛选手提供了7类数据：\n",
    "\n",
    "1）加热棒上部设定温度T1-1\\~T1-17\n",
    "\n",
    "2）加热棒下部设定温度T2-1\\~T2-17\n",
    "\n",
    "3）进气口的设定进气流量V1-V17\n",
    "\n",
    "4）上部测温点实际温度T3-1\\~T3-17\n",
    "\n",
    "5）下部测温点实际温度T4-1\\~T4-17\n",
    "\n",
    "6）匣钵上部实际温度T5-1\\~T5-17\n",
    "\n",
    "7）匣钵下部实际温度T6-1\\~T6-17\n",
    "\n",
    "表中温度单位均为摄氏度℃，流量单位均为L/min。\n",
    "\n",
    "## 2.评估指标\n",
    "\n",
    "初赛（任务一）采用RMSE作为评价指标，最终得分为测试集1和测试集2的平均值。取前10名进入复赛。\n",
    "\n",
    "训练集1为含有前5类数据的训练数据；训练集2含有全部7类数据。由于训练集2中数据较少，选手需要首先使用训练集1进行时空建模并预测实际温度，校验时空模型的准确性，再使用训练集2对含有匣钵实际温度的时空模型进行建模并预测匣钵实际温度，以便达到更高的准确性。\n",
    "\n",
    "测试集1采用加热棒上部设定温度T1-1\\~T1-17、加热棒下部设定温度T2-1\\~T2-17、进气口的设定进气流量V1-V17这3类数据作为输入，选手分别预测上部测温点实际温度T’3-1\\~T’3-17、下部测温点实际温度T’4-1\\~T’4-17。\n",
    "\n",
    "测试集2采用加热棒上部设定温度T1-1\\~T1-17、加热棒下部设定温度T2-1\\~T2-17、进气口的设定进气流量V1-V17这3类数据作为输入，选手分别预测上部测温点实际温度T’3-1\\~T’3-17、下部测温点实际温度T’4-1\\~T’4-17、匣钵上部实际温度T’5-1\\~T’5-17、匣钵下部实际温度T’6-1\\~T’6-17。\n",
    "\n",
    "复赛（任务二）以匣钵上部实际温度T5-1\\~T5-17、匣钵下部实际温度T6-1\\~T6-17作为目标，选手需给出从初始时刻到达到目标值的加热过程中加热棒上部设定温度、加热棒下部设定温度和进气口的设定进气流量的逐秒设定值。选手须提交预测结果xlsx表格，以及相应的代码、模型和说明文档，赛题方对结果进行人工评审，确定最终排名。\n",
    "\n",
    "例如：初始时刻：加热棒上部设定温度T1-1为30、加热棒下部设定温度T2-1为30、上部测温点实际温度T3-1为30、下部测温点实际温度T4-1为30、进气口的设定进气流量V1为10，假设此时匣钵上部实际温度为T’5-1、匣钵下部实际温度为T’6-1；目标值：匣钵上部实际温度T5-1为600、匣钵下部实际温度T6-1为600。选手需要制定加热的控制策略，将匣钵实际温度从T’5-1、T’6-1加热到T5-1 、T6-1，并将加热过程中加热棒上部设定温度、加热棒下部设定温度和进气口的设定进气流量的设定值以及匣钵上部实际温度T‘5-1\\~T’5-17、匣钵下部实际温度T‘6-1\\~T’6-17、上部测温点实际温度T’3-1\\~T’3-17、下部测温点实际温度T’4-1\\~T’4-17逐秒输出成表。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a25f9589-3470-4c5f-bff6-631741946be0",
   "metadata": {},
   "outputs": [],
   "source": [
    "!unzip data.zip\n",
    "!unzip submit.zip\n",
    "!rm submit.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "487b3ffb-10f8-4b8a-8576-fc97f9d32ed8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "\n",
    "import pandas as pd\n",
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "16a89168-4418-4b31-a62f-26774581250c",
   "metadata": {},
   "outputs": [],
   "source": [
    "train1 = pd.read_csv('./dataset/train/train1.csv')\n",
    "train2 = pd.read_csv('./dataset/train/train2.csv')\n",
    "\n",
    "test1 = pd.read_csv('./dataset/test/test1.csv')\n",
    "test2 = pd.read_csv('./dataset/test/test2.csv')\n",
    "\n",
    "sub1 = pd.read_csv('./submit/submit1.csv')\n",
    "sub2 = pd.read_csv('./submit/submit2.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "df9f42cb-af30-47cd-91fb-a5657288d346",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3133.795971155943"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred = cross_val_predict(\n",
    "    LinearRegression(),\n",
    "    train1.iloc[:, 2:53],\n",
    "    train1.iloc[:, 53:],\n",
    ")\n",
    "\n",
    "mean_squared_error(train1.iloc[:, 53:], pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b0ad414c-ea64-4671-adb8-30733b359790",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4276.647753819876"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred = cross_val_predict(\n",
    "    KNeighborsRegressor(),\n",
    "    train1.iloc[:, 2:53],\n",
    "    train1.iloc[:, 53:],\n",
    ")\n",
    "\n",
    "mean_squared_error(train1.iloc[:, 53:], pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8996f5ce-8392-4b18-b17c-35138b2c13fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "model1 = LinearRegression()\n",
    "model1.fit(train1.iloc[:, 2:53], train1.iloc[:, 53:])\n",
    "sub1.iloc[:, 1:] = model1.predict(test1.iloc[:, 2:53]).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6708c9b6-7783-4503-81cb-48541a85d5db",
   "metadata": {},
   "outputs": [],
   "source": [
    "model1 = LinearRegression()\n",
    "model1.fit(train2.iloc[:, 2:53], train2.iloc[:, 53:])\n",
    "sub2.iloc[:, 1:] = model1.predict(test2.iloc[:, 2:53]).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "3df7d57d-fdef-4d8f-9b05-e0f86b3dfdaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "sub1.to_csv('./submit/submit1.csv', index=None)\n",
    "sub2.to_csv('./submit/submit2.csv', index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ec39039b-b224-492f-a661-7dde70ac699d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  adding: submit/ (stored 0%)\n",
      "  adding: submit/submit1.csv (deflated 94%)\n",
      "  adding: submit/submit2.csv (deflated 59%)\n"
     ]
    }
   ],
   "source": [
    "!zip -r submit.zip submit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc537b31-37b7-43c1-a968-01f7c7598ab0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3.11",
   "language": "python",
   "name": "py3.11"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
